﻿Imports Microsoft.Office.Interop
Public Class fProveedores

    Dim daDatosTablas As lbGlobalcom.dsGlobalTableAdapters.GLO_DATOSTABLASTableAdapter
    Dim daProveedor As lbGlobalcom.dsTerceroTableAdapters.TER_PROVEEDORESTableAdapter
    Dim daFP As lbGlobalcom.dsCarteraTableAdapters.CAR_FORMASPAGOTableAdapter
    Private Sub fProveedores_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        daDatosTablas = New lbGlobalcom.dsGlobalTableAdapters.GLO_DATOSTABLASTableAdapter
        daProveedor = New lbGlobalcom.dsTerceroTableAdapters.TER_PROVEEDORESTableAdapter
        daFP = New lbGlobalcom.dsCarteraTableAdapters.CAR_FORMASPAGOTableAdapter
        MyBase.MustraMsjSalva = False
        AddHandler Me.SalvaTerecero, AddressOf Salvar
        AddHandler Me.BuscaTercero, AddressOf BuscaProveedor
        AddHandler Me.EliminaTercero, AddressOf EliminaProveedor
        AddHandler Me.AddTercero, AddressOf AddProveedor
        AddHandler Me.ExporToExcel, AddressOf ExportProToExcel
        TER_TERCEROBindingNavigator.Items("BindingNavigatorAddNewItem").Enabled = False
        TER_TERCEROBindingNavigator.Items("BindingNavigatorDeleteItem").Enabled = False
        Try
            daDatosTablas.FillByIdGrupo(DsGlobal.GLO_DATOSTABLAS, "es")
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Private Sub ExportProToExcel()
        Try
            Me.Cursor = Cursors.WaitCursor
            Dim cex As New lbGlobalcom.cExcel(lbGlobalcom.DatosTerceros.GetProveedores())
            Dim m_Excel As Excel.Application
            m_Excel = cex.exportarExcel
            m_Excel.Visible = True
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            Me.Cursor = Cursors.Default
        End Try
    End Sub
    Private Sub ActivaEstadoProveeedor(ByVal ValorEstado As Boolean)
        IDFORMAPAGOTextBox.Enabled = ValorEstado
        ESTADOListBox1.Enabled = ValorEstado
        CUPOTextBox.Enabled = ValorEstado
        EMAILTextBox.Enabled = ValorEstado
        PAGWEBTextBox.Enabled = ValorEstado
        btnBuscaFP.Enabled = ValorEstado
    End Sub
    Private Sub AddProveedor()
        Try
            Dim drter As lbGlobalcom.dsTercero.TER_PROVEEDORESRow = DsTercero.TER_PROVEEDORES.NewTER_PROVEEDORESRow
            drter.IDPROVEEDOR = IDTERCEROTextBox.Text
            DsTercero.TER_PROVEEDORES.Rows.Add(drter)
            drter = Nothing
            ActivaEstadoProveeedor(True)
            'TER_PROVEEDORESBindingSource.AddNew()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Private Sub EliminaProveedor()
        Try
            TER_PROVEEDORESBindingSource.Remove(TER_PROVEEDORESBindingSource.Current)
            daProveedor.Update(DsTercero.TER_PROVEEDORES)
            ActivaEstadoProveeedor(False)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Private Sub Salvar()
        Try
            TER_PROVEEDORESBindingSource.EndEdit()
            daProveedor.Update(DsTercero.TER_PROVEEDORES)
            MessageBox.Show("Se salvo registgro con exito", "Salva Registro", MessageBoxButtons.OK)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Private Sub BuscaProveedor(ByVal idProveedor As String)
        Try
            If DsTercero.TER_PROVEEDORES.GetChanges IsNot Nothing Then
                If MessageBox.Show("Hay cambios pendientes por salvar, desea salvarlos", "Cambios Pendientes", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                    daProveedor.Update(DsTercero.TER_PROVEEDORES)
                End If
            End If
            daProveedor.FillByIdProveedor(DsTercero.TER_PROVEEDORES, idProveedor)
            If DsTercero.TER_PROVEEDORES.Rows.Count = 0 Then
                ActivaEstadoProveeedor(False)
            Else
                ActivaEstadoProveeedor(True)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub btnBuscaFP_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaFP.Click
        Dim fBusFP As New fListaFormasPago
        If fBusFP.ShowDialog = Windows.Forms.DialogResult.OK Then
            IDFORMAPAGOTextBox.Text = fBusFP.FP
        End If
    End Sub

    Private Sub IDFORMAPAGOTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IDFORMAPAGOTextBox.TextChanged
        Try
            If IDFORMAPAGOTextBox.Text.Trim.Length > 0 Then
                Me.lbFormasPago.Text = daFP.GetDescripcionFormaPago(IDFORMAPAGOTextBox.Text)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        AddProveedor()
    End Sub

    Private Sub btnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDel.Click
        If MessageBox.Show("Esta seguro que desea eliminar este proveedor", "Elimina Proveedor", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
            EliminaProveedor()
        End If
    End Sub
End Class
